package com.yc.demo.d0316;

import com.google.gson.Gson;
import com.yc.demo.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveBookServlet", value = "/SaveBookServlet.s")
public class SaveBookServlet extends HttpServlet {
    private BookBiz biz = new BookBiz();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookname = request.getParameter("bookname");
        String bookauthor = request.getParameter("bookauthor");
        String bookpress = request.getParameter("bookpress");
        String pressdate = request.getParameter("pressdate");
        String bookimage = request.getParameter("bookimage");
        String status = request.getParameter("status");
        String id = request.getParameter("id");

        Result result;
        try {
           if (id==null){
               biz.create(bookname,bookauthor,bookpress,pressdate,bookimage);
           }else{
               biz.modify(id,bookname,bookauthor,bookpress,pressdate,bookimage,status);
           }
            result = new Result(1,"图书保存成功",null);
        } catch (BizException e) {
            e.printStackTrace();
            result = new Result(0,e.getMessage(),null);
        }

        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
